package com.knowif.backend.controller;

import com.github.pagehelper.PageInfo;
import com.knowif.backend.service.FastAskService;
import com.knowif.search.SearchCondition;
import com.knowif.system.model.RestBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/fastAsk")
public class FastAskOrderManagerController {

    @Autowired
    private FastAskService fastAskService;

    @PostMapping("/screen")
    public RestBean screen(@RequestBody SearchCondition searchCondition){
        try {
            int pageSize = searchCondition.getPageSize();
            int pageNumber = searchCondition.getPageNumber();
            Map<String, Object> params = searchCondition.getParameterMap();
            if(params.containsKey("price")){
                try {
                    Double price = (Double.parseDouble((String) params.get("price")));
                    params.put("price", price);
                } catch (NumberFormatException e) {
                    return RestBean.buildError("请输入正确金额格式");
                }
            }
            PageInfo pageInfo = fastAskService.screen(params, pageNumber, pageSize);
            return RestBean.buildSuccess(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

}
